package gitlet;

import java.io.IOException;

/**
 * 可以用于命令行输入的类
 *
 * @author 王子琪、杨凯伟
 * @date 2023/11/16
 */
public class Backend {
    /**
     * 用命令行运行程序
     *
     * @param cmd  命令集
     * @param repo 仓库类实例
     * @author 王子琪、杨凯伟
     * @date 2023/11/16
     */
    public static void run(String[] cmd, Repository repo) {
        System.out.print(System.getProperty("user.name") + "$ : (" + repo.getCurrentBranch() + ")");
        StringBuilder c = new StringBuilder();
        for (String s : cmd)
            c.append(s).append(" ");
        System.out.println(c);
        if (cmd.length == 0) {
            System.out.println("请输入一个命令");
        }
        switch (cmd[0]) {
            case "log" -> repo.log(cmd);
            case "add" -> repo.add(cmd);
            case "rm" -> repo.remove(cmd);
            case "commit" -> repo.commit(cmd);
            case "status" -> repo.status(cmd);
            case "checkout" -> repo.checkout(cmd);
            case "branch" -> repo.branch(cmd);
            case "rm-branch" -> repo.removeBranch(cmd);
            case "reset" -> repo.reset(cmd);
            case "find" -> repo.find(cmd);
            case "global-log" -> repo.globalLog(cmd);
            case "merge" -> repo.merge(cmd);
            case "add-remote" -> repo.remoteAdd(cmd);
            case "remove-remote" -> repo.remoteRemove(cmd);
            case "fetch" -> {
                try {
                    repo.fetch(cmd);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            case "push" -> {
                try {
                    repo.push(cmd);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            case "pull" -> {
                try {
                    repo.pull(cmd);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            case "clone" -> {
                try {
                    repo.Clone(cmd);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            case "quit" -> repo.quit(cmd);
            default -> System.out.println("没有此命令！");
        }
    }
}